//
//  CREssenceViewController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/27.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CREssenceViewController.h"
#import "CRTitleButton.h"
#import "CRVideoViewController.h"
#import "CRVoiceViewController.h"
#import "CRAllViewController.h"
#import "CRPictureViewController.h"
#import "CRWordViewController.h"
@interface CREssenceViewController () <UIScrollViewDelegate>
/** scrollView: 所有tableView的父控件 */
@property (nonatomic, weak) UIScrollView *scrollView;
/** 所有标题的父控件 */
@property (nonatomic, weak) UIView *titlesView;
/** 标题底部的指示线（下划线） */
@property (nonatomic, weak) UIView *titleUnderlineView;
/** 记录上一次点击的标题按钮 */
@property (nonatomic, weak) UIButton *clickedTitleButton;
/** 存放所有的标题按钮 */
@property (nonatomic, strong) NSMutableArray *titleButtons;


@property (nonatomic, strong) NSMutableArray *viewArr;
@end

@implementation CREssenceViewController
#pragma mark >>>> 懒加载

- (NSMutableArray *)titleButtons {
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray new];
    }
    return _titleButtons;
}

#pragma mark >>>> 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    // 自定义控件初始化
    [self setupSubViews];
    
    // 初始化数据模型
    [self initData];
    // 设置全部子控制器
    [self setupAllChildVCs];
    // 默认显示第0个控制器的view
    [self scrollViewDidEndScrollingAnimation:self.scrollView];
    
}

#pragma mark >>>> 初始化方法
/**
 *  自定义控件初始化
 */
- (void)setupSubViews {
    // 添加scrollView
    [self setupScrollView];
    // 添加标题栏
    [self setupTitlesView];
}
/**
 *  设置全部子控制器
 */
- (void)setupAllChildVCs {
    CRAllViewController *all = [[CRAllViewController alloc] init];
    all.title = @"全部";
    [self addChildViewController:all];
    
    CRVideoViewController *video = [[CRVideoViewController alloc] init];
    video.title = @"视频";
    [self addChildViewController:video];
    
    CRVoiceViewController *voice = [[CRVoiceViewController alloc] init];
    voice.title = @"声音";
    [self addChildViewController:voice];
    
    CRPictureViewController *picture = [[CRPictureViewController alloc] init];
    picture.title = @"图片";
    [self addChildViewController:picture];
    
    CRWordViewController *word = [[CRWordViewController alloc] init];
    word.title = @"段子";
    [self addChildViewController:word];
    
    // 设置contentSize
    CGFloat contentW = self.childViewControllers.count * self.scrollView.width;
    self.scrollView.contentSize = CGSizeMake(contentW, 0);
}
/**
 *  初始化数据模型
 */
- (void)initData {
    
}

#pragma mark ---- 添加标题栏
- (void)setupTitlesView {
    // 标题栏
    UIView *titlesView = [[UIView alloc] init];
    titlesView.x = 0;
    titlesView.y = kDefaultNavigationBarMaxY;
    titlesView.width = 375;
    titlesView.height = kDefaultTitlesViewHeight;
    [titlesView setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.5]];
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 标题底部的下划线
    UIView *titleUnderlineView = [[UIView alloc] init];
    titleUnderlineView.height = 1;
    titleUnderlineView.y = titlesView.height - titleUnderlineView.height;
    [titleUnderlineView setBackgroundColor:[UIColor redColor]];
    [titlesView addSubview:titleUnderlineView];
    self.titleUnderlineView = titleUnderlineView;
    
    // 文字
    NSArray *titleArray = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
    CGFloat titleBW = titlesView.width / titleArray.count;
    CGFloat titleBH = titlesView.height;
    
    for (int i = 0; i < titleArray.count; i++) {
        // 创建标题按钮
        CRTitleButton *titleButton = [CRTitleButton buttonWithType:UIButtonTypeCustom];
        
        // 设置tag值
        titleButton.tag = i;
        
        // 设置frame
        CGFloat titleBX = i * titleBW;
        CGFloat titleBY = 0;
        titleButton.frame = CGRectMake(titleBX, titleBY, titleBW, titleBH);
        
        // 添加点击事件
        [titleButton addTarget:self action:@selector(didClcikTitleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 设置文字
        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
        [titlesView addSubview:titleButton];
        [self.titleButtons addObject:titleButton];
        
        if (0 == i) {
            // 默认选择第0个按钮
            titleButton.selected = YES;
            self.clickedTitleButton = titleButton;
            // 马上根据文字设置内部label的尺寸
            [titleButton.titleLabel sizeToFit];
            self.titleUnderlineView.width = titleButton.titleLabel.width;
            self.titleUnderlineView.centerX = titleButton.centerX;
        }
        
    }
}

#pragma mark ---- 添加scrollView
- (void)setupScrollView {
    // 不要自动调整scrollView的inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
}

#pragma mark >>>> 事件监听方法
- (void)didClcikTitleButtonAction:(UIButton *)button {
    // 修改按钮状态
    self.clickedTitleButton.selected = NO;
    button.selected = YES;
    self.clickedTitleButton = button;
    
    // 移除底部下划线
    [UIView animateWithDuration:0.15 animations:^{
        self.titleUnderlineView.width = button.titleLabel.width;
        self.titleUnderlineView.centerX = button.centerX;
    }];
    
    // 让scrollView滚动到对应的位置(不要去修改contentOffset的y值)
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = button.tag * self.scrollView.width;
    [self.scrollView setContentOffset:offset animated:YES];
    
}

#pragma mark >>>> 实现代理方法
/**
 *  滚动完毕就会调用（如果不是人为拖拽scrollView导致滚动完毕，才会调用这个方法）
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    int index = scrollView.contentOffset.x / scrollView.width;
    
    UIViewController *willShowChildVc = self.childViewControllers[index];
    // 如果这个子控制器的view已经添加过了，就直接返回
    if (willShowChildVc.isViewLoaded) return;
    // 添加子控制器的view
    willShowChildVc.view.frame = scrollView.bounds;
    
    [scrollView addSubview:willShowChildVc.view];
}


/**
 *  滚动完毕就会调用（如果是人为拖拽scrollView导致滚动完毕，才会调用这个方法）
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int index = scrollView.contentOffset.x / scrollView.width;
    // 点击对应的按钮
    [self didClcikTitleButtonAction:self.titleButtons[index]];
    
    // 添加子控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
}

#pragma mark >>>> 其他方法


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
